package com.yowasa.xiuxian.engine.item.lingyao;

import cn.hutool.core.util.RandomUtil;
import com.yowasa.xiuxian.annoations.Item;
import com.yowasa.xiuxian.engine.item.ItemInterface;
import com.yowasa.xiuxian.entity.bo.user.UserAttr;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.ItemTypeEnum;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Field;

@Item(name = "神形再造丹", type = ItemTypeEnum.LING_YAO, price = 20000)
public class ShenXingZaiZaoDan implements ItemInterface {
    @Autowired
    private UserHelper userHelper;

    @Override
    public String desc() {
        return "重随基础属性,每项属性都会随机增加或减少，最高5点";
    }

    @Override
    public String effect(UserBO userBO) {
        UserAttr userAttr = userBO.getUserAttr();
        userAttr.setLuc(userAttr.getLuc());

        Class<UserAttr> userAttrClass = UserAttr.class;
        Field[] declaredFields = userAttrClass.getDeclaredFields();
        try {
            for (Field field : declaredFields){
                field.setAccessible(true);
                double random = Math.random();
                int o = (int) field.get(userAttr);
                int i = RandomUtil.randomInt(5) + 1;
                if(random > 0.6){
                    field.set(userAttr,Math.max(o-i,0));
                }else {
                    field.set(userAttr,Math.min(o+i,100));
                }
            }
        }catch (Exception e){
            throw new HandlerException("出现了未知错误，请联系开发者");
        }

        userBO.setUserAttr(userAttr);
        userHelper.calFeiShengAttrUpper(userBO);
        return "身体属性已经重制,请自行查看";
    }




}
